package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.CategoryBrandService;
import com.spzx.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author GQS @Date 2024/9/20/15:13 @Description
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand>
    implements CategoryBrandService {
  @Autowired private CategoryBrandMapper categoryBrandMapper;
  @Autowired private CategoryService categoryService;

  /**
   * 查询分类品牌列表
   *
   * @param categoryBrand 分类品牌
   * @return 分类品牌
   */
  @Override
  public List<CategoryBrand> selectCategoryBrandList(CategoryBrand categoryBrand) {
    return categoryBrandMapper.selectCategoryBrandList(categoryBrand);
  }

  @Override
  public CategoryBrand selectCategoryBrandById(Long id) {
    // 根据ID获取分类品牌实体
    CategoryBrand categoryBrand = this.getById(id);

    // 获取当前品牌的分类ID对应的全部父分类ID列表
    List<Long> categoryIdList = categoryService.getAllCategoryIdList(categoryBrand.getCategoryId());

    // 设置分类ID列表到分类品牌实体中
    categoryBrand.setCategoryIdList(categoryIdList);

    // 返回包含分类ID列表的分类品牌实体
    return categoryBrand;
  }

  @Transactional(rollbackFor = Exception.class)
  @Override
  public int insertCategoryBrand(CategoryBrand categoryBrand) {
    // 同一分类下禁止添加相同品牌
    long count =
        categoryBrandMapper.selectCount(
            new LambdaQueryWrapper<CategoryBrand>()
                .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId()));
    if (count > 0) {
      throw new ServiceException("该分类已加该品牌");
    }
    return categoryBrandMapper.insert(categoryBrand);
  }

  @Override
  public int updateCategoryBrand(CategoryBrand categoryBrand) {
    // 同一分类下禁止更改已有的品牌

    // CategoryBrand one =
    //     this.getOne(
    //         Wrappers.lambdaQuery(CategoryBrand.class)
    //             .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId())
    //             .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
    //             .last("limit 1"));
    // if (one != null && !one.getId().equals(categoryBrand.getId())) {
    //   throw new ServiceException("该分类已有该品牌");
    // }

    // 写法二
    // 通过ID获取原始的CategoryBrand对象
    CategoryBrand originalCategoryBrand = this.getById(categoryBrand.getId());

    // 检查更新后的CategoryBrand对象与原始对象的categoryId和brandId是否发生了变化
    if (categoryBrand.getCategoryId().longValue()
            != originalCategoryBrand.getCategoryId().longValue()
        || categoryBrand.getBrandId().longValue()
            != originalCategoryBrand.getBrandId().longValue()) {
      // 查询当前categoryId和brandId组合是否存在除当前对象外的其他记录
      long count =
          categoryBrandMapper.selectCount(
              new LambdaQueryWrapper<CategoryBrand>()
                  .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
                  .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId()));

      // 如果存在相同的categoryId和brandId组合，抛出异常
      if (count > 0) {
        throw new ServiceException("该分类已加该品牌");
      }
    }

    return categoryBrandMapper.updateById(categoryBrand);
  }

  @Override
  public int removeBatchByIds(List<Long> list) {
    return categoryBrandMapper.deleteBatchByIds(list);
  }

  @Override
  public List<Brand> selectBrandListByCategoryId(Long categoryId) {
    return categoryBrandMapper.selectBrandListByCategoryId(categoryId);
  }
}
